﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.Common;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 有机硫。审核
    /// </summary>
    /// 创建时间：2022-12-27 18:01:08。作者：王浩力
    public partial class WindowSulfurCheck : Window
    {
        public WindowSulfurCheck()
        {
            InitializeComponent();
        }
        ISource_dictDAL source_DictDAL = ServicesHelpter.GetService<ISource_dictDAL>();
        SulfurBusiness business = new SulfurBusiness();
        UserBusiness userBusiness = new UserBusiness();

        /// <summary>
        /// 刷新父页面分页数据
        /// </summary>
        internal event EventHandler RefreshPage;

        /// <summary>
        /// 被审核的 Organic_sulfur
        /// </summary>
        internal Organic_sulfur editInfo;

        SourceDictBusiness sourceBusiness = new SourceDictBusiness();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var all = source_DictDAL.GetAll();
            all = all.FindAll(g => g.D_stat == 1);

            //人
            var allUser = userBusiness.GetAllUser();
            allUser = allUser.FindAll(g => g.U_stat == 1);

            //取样条件
            cmb_Sampling_conditions.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyangCondition"));
            cmb_Sampling_conditions.DisplayMemberPath = "Item_name";
            cmb_Sampling_conditions.SelectedValuePath = "Id";

            //委托单位
            cmb_Client.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Client.DisplayMemberPath = "Item_name";
            cmb_Client.SelectedValuePath = "Id";

            //取样人
            cmb_Sampling_user_name.ItemsSource = allUser;
            cmb_Sampling_user_name.DisplayMemberPath = "User_real_name";
            cmb_Sampling_user_name.SelectedValuePath = "Id";

            //分析人
            cmb_Analysis_userID.ItemsSource = allUser;
            cmb_Analysis_userID.DisplayMemberPath = "User_real_name";
            cmb_Analysis_userID.SelectedValuePath = "Id";

            //取样单位
            cmb_Sampling_unit.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Sampling_unit.DisplayMemberPath = "Item_name";
            cmb_Sampling_unit.SelectedValuePath = "Id";

            //层位
            cmb_Horizon.ItemsSource = all.FindAll(g => g.Group_id.Equals("cengwei"));
            cmb_Horizon.DisplayMemberPath = "Item_name";
            cmb_Horizon.SelectedValuePath = "Id";

            //分析单位
            cmb_Analysis_unit.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Analysis_unit.DisplayMemberPath = "Item_name";
            cmb_Analysis_unit.SelectedValuePath = "Id";

            //取样位置
            cmb_Sampling_location.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyangLocation"));
            cmb_Sampling_location.DisplayMemberPath = "Item_name";
            cmb_Sampling_location.SelectedValuePath = "Id";

            //--------------------------------------数据编辑赋值--------------------------------------
            //日期时间初始化
            date_Analysis_time.Text = editInfo.Analysis_time;
            date_Sampling_time.Text = editInfo.Sampling_time;
            txtSamp_no.Text = editInfo.Samp_no;
            txtWell_deep.Text = editInfo.Well_deep.ToString();

            cmb_Sampling_conditions.SelectedValue = editInfo.Sampling_conditions ?? null;
            cmb_Client.SelectedValue = editInfo.Client ?? null;
            cmb_Sampling_user_name.SelectedValue = editInfo.Sampling_user_name ?? null;
            cmb_Sampling_unit.SelectedValue = editInfo.Sampling_unit ?? null;

            //分析人id。添加时间：2023-1-9 17:19:02。
            cmb_Analysis_userID.SelectedValue = editInfo.Analysis_userID ?? null;

            txtWell_no.Text = editInfo.Well_no;
            if (!string.IsNullOrWhiteSpace(editInfo.Horizon))
            {
                cmb_Horizon.SelectedValue = editInfo.Horizon;
            }

            txtOil_pressure.Text = editInfo.Oil_pressure.ToString();
            cmb_Analysis_unit.SelectedValue = editInfo.Analysis_unit ?? null;

            
            //取样位置；修改时间：2023-1-15 21:26:14。
            if (!string.IsNullOrWhiteSpace(editInfo.Sampling_location))
            {
                if (Regex.IsMatch(editInfo.Sampling_location, "^\\d{1,19}"))
                {
                    cmb_Sampling_location.SelectedValue = editInfo.Sampling_location;
                }
                else
                {
                    txt_Sampling_location.Text = editInfo.Sampling_location;
                }
            }

            txtSampling_temp.Text = editInfo.Sampling_temp.ToString();
            txtCasing_pressure.Text = editInfo.Casing_pressure.ToString();

            s_Cos.Text = editInfo.Cos.ToString();
            s_Dimethyl_Sulfide.Text = editInfo.Dimethyl_Sulfide.ToString();
            s_Tert_butyl.Text = editInfo.Tert_butyl.ToString();
            s_Diethyl_sulphide.Text = editInfo.Diethyl_sulphide.ToString();

            s_Methyl_mercaptan.Text = editInfo.Methyl_mercaptan.ToString();
            s_Carbon_disulfide.Text = editInfo.Carbon_disulfide.ToString();
            s_Methylethyl_sulfide.Text = editInfo.Methylethyl_sulfide.ToString();
            s_N_Butyl_mercaptan.Text = editInfo.N_Butyl_mercaptan.ToString();

            s_Ethanethiol.Text = editInfo.Ethanethiol.ToString();
            s_Isopropanethiol.Text = editInfo.Isopropanethiol.ToString();
            s_Thiophene.Text = editInfo.Thiophene.ToString();
            s_Dithioether.Text = editInfo.Dithioether.ToString();

            s_total.Content = editInfo.Organic_sulfur_total.ToString();
            txtRemark.Text = editInfo.Remark;


            //设置所有输入控件只读
            #region 设置所有输入控件只读
            var grid = (DependencyObject)this.Content;
            List<DependencyObject> list = new List<DependencyObject>();
            Action<DependencyObject> action = null;
            action = (a) =>
            {
                var count = VisualTreeHelper.GetChildrenCount(a);
                for (int i = 0; i < count; i++)
                {
                    var elem = VisualTreeHelper.GetChild(a, i);
                    list.Add(elem);
                    action(elem);
                }
            };

            action(grid);

            foreach (var item in list)
            {
                if (item is TextBox)
                {
                    var elem = (item as TextBox);
                    if (elem.Name.Equals("txtRejectReason"))
                    {
                        continue;
                    }
                    (item as TextBox).IsEnabled = false; //批量设置输入框禁用
                }
                else if (item is ComboBox)
                {
                    (item as ComboBox).IsEnabled = false;
                }
            }

            #endregion

        }

        //驳回
        private void Reject_Click(object sender, RoutedEventArgs e)
        {
            Organic_sulfur_check_history organic_Sulfur_Check_ = new Organic_sulfur_check_history();
            organic_Sulfur_Check_.Check_stat = 0;
            organic_Sulfur_Check_.Reject_reason = txtRejectReason.Text;
            organic_Sulfur_Check_.Organic_sulfurtId_ID = editInfo.Id;
            var result = business.Check(organic_Sulfur_Check_);
            if (result.CodeEnum == ResultCode.Success)
            {
                btnReject.IsEnabled = false;
                btnConfirm.IsEnabled = false;
                RefreshPage?.Invoke(this, e);
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //审核通过
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            Organic_sulfur_check_history organic_Sulfur_Check_ = new Organic_sulfur_check_history();
            organic_Sulfur_Check_.Check_stat = 1;
            organic_Sulfur_Check_.Organic_sulfurtId_ID = editInfo.Id;
            var result = business.Check(organic_Sulfur_Check_);
            if (result.CodeEnum == ResultCode.Success)
            {
                btnReject.IsEnabled = false;
                btnConfirm.IsEnabled = false;
                RefreshPage?.Invoke(this, e);

                // 审核通过的，生成PDF文件 //创建时间：2023-1-12 10:39:15
                //查询完整数据         
                OrganicSulfurView model = business.GetView(editInfo.Id);

                //备注 ，标准。创建时间：2023-1-13 13:53:59
                var biaozhunList = sourceBusiness.GetListByGroup("sLiu_bz_remark_list", 1);
                biaozhunList = biaozhunList.OrderBy(g => g.Createtime).ToList();
                model.source_Dicts = biaozhunList;

                string pdfFileName = $"有机硫{editInfo.Samp_no}井{editInfo.Well_no}";
                MakePdfHelpter.CheckPassLoadXps(pdfFileName, "FlowDocumentSulfurPrint.xaml", model,
                    () =>
                    {
                        string dir = DateTime.Now.ToString("yyyy");
                        dir = System.IO.Path.Combine("有机硫", dir);
                        return dir;
                    });
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //退出
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtRejectReason_GotFocus(object sender, RoutedEventArgs e)
        {
            this.txtRejectReason.Text = "";
        }
    }
}
